package com.sskj.gcc.util;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import com.afollestad.materialdialogs.MaterialDialog;
import com.sskj.common.base.App;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.gcc.R;

public class AppTipUtil {
    /**
     * 天天挖矿  右上角展示文本
     */
    public static void showTitleNotice(Context context, String msg) {
        MaterialDialog dialog = new MaterialDialog.Builder(context)
                .customView(R.layout.app_yuanli_dialog_notice, false)
                .autoDismiss(false)
                .backgroundColor(Color.TRANSPARENT)
                .cancelable(false)
                .show();
        View customView = dialog.getCustomView();
        TextView tvContent = customView.findViewById(R.id.tvContent);
        tvContent.setText(msg);
        ((ViewGroup) (customView.getParent())).setBackgroundColor(Color.TRANSPARENT);
        ((ViewGroup) (customView.getParent().getParent())).setBackgroundColor(Color.TRANSPARENT);
        ((ViewGroup) (customView.getParent().getParent().getParent())).setBackgroundColor(Color.TRANSPARENT);
        ((ViewGroup) (customView.getParent().getParent().getParent().getParent())).setBackgroundColor(Color.TRANSPARENT);
        ((ViewGroup) (customView.getParent().getParent().getParent().getParent().getParent())).setBackgroundColor(Color.TRANSPARENT);
        ClickUtil.click(customView.findViewById(R.id.btSubmit), () -> {
            dialog.dismiss();
        });

    }

    public static void showDialog(Context context, OnTipSure onTipPwdSure, String msg) {
        MaterialDialog dialog = new MaterialDialog.Builder(context)
                .customView(R.layout.app_yuanli_dialog, false)
                .autoDismiss(false)
                .backgroundColor(Color.TRANSPARENT)
                .cancelable(false)
                .show();
        View customView = dialog.getCustomView();
        TextView tvCancel = customView.findViewById(R.id.tvCancel);
        TextView tvContent = customView.findViewById(R.id.tvContent);
        tvContent.setText(msg);
        ((ViewGroup) (customView.getParent())).setBackgroundColor(Color.TRANSPARENT);
        ((ViewGroup) (customView.getParent().getParent())).setBackgroundColor(Color.TRANSPARENT);
        ((ViewGroup) (customView.getParent().getParent().getParent())).setBackgroundColor(Color.TRANSPARENT);
        ((ViewGroup) (customView.getParent().getParent().getParent().getParent())).setBackgroundColor(Color.TRANSPARENT);
        ((ViewGroup) (customView.getParent().getParent().getParent().getParent().getParent())).setBackgroundColor(Color.TRANSPARENT);

        ClickUtil.click(customView.findViewById(R.id.btSubmit), () -> {
            onTipPwdSure.onSure();
        });
        ClickUtil.click(customView.findViewById(R.id.viewCancel), () -> {
            dialog.dismiss();
        });
        ClickUtil.click(tvCancel, () -> {
            dialog.dismiss();
        });
    }

    public static MaterialDialog showGoogleCheckDialog(Context context, OnTipPwdSure onTipPwdSure) {
        if (context instanceof Activity) {
            if (((Activity) context).isFinishing()) {
                return null;
            }
        }
        MaterialDialog dialog = new MaterialDialog.Builder(context)
                .customView(R.layout.app_dialog_google_check, false)
                .autoDismiss(false)
                .backgroundColor(Color.TRANSPARENT)
                .cancelable(false)
                .show();
        View customView = dialog.getCustomView();
        TextView tvCancel = customView.findViewById(R.id.tvCancel);
        EditText etPwd = customView.findViewById(R.id.etPwd);
        ((ViewGroup) (customView.getParent())).setBackgroundColor(Color.TRANSPARENT);
        ((ViewGroup) (customView.getParent().getParent())).setBackgroundColor(Color.TRANSPARENT);
        ((ViewGroup) (customView.getParent().getParent().getParent())).setBackgroundColor(Color.TRANSPARENT);
        ((ViewGroup) (customView.getParent().getParent().getParent().getParent())).setBackgroundColor(Color.TRANSPARENT);
        ((ViewGroup) (customView.getParent().getParent().getParent().getParent().getParent())).setBackgroundColor(Color.TRANSPARENT);

        ClickUtil.click(customView.findViewById(R.id.btSubmit), () -> {
            if (TextUtils.isEmpty(etPwd.getText())) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.appJavaAppTipUtil1));
                return;
            }
            onTipPwdSure.onSure(etPwd.getText().toString());
        });
        ClickUtil.click(tvCancel, () -> {
            dialog.dismiss();
        });
        return dialog;
    }

    public interface OnTipSure {
        void onSure();
    }

    public interface OnTipPwdSure {
        void onSure(String pwd);
    }

    public static boolean isActivityAlive(Activity activity) {
        return activity != null && !activity.isFinishing()
                && (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1 || !activity.isDestroyed());
    }
}
